/*
 * Copyright (C) 2010 The Android Open Source Project 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at 
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */

package org.lhlzi.ithink;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.ContentObserver;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.provider.CallLog;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;



public class RevealService extends Service {
	public final static String TAG = RevealService.class.toString();
	
	private static DataCacheWrapper dataCacheWrapper = DataCacheWrapper.getInstance();

	//protected Map states = new HashMap();

	//protected NumberDatabase numberdb;

	protected TelephonyManager teleman;
	private NotificationLooper noteLoop;

	protected PhoneStateListener listener = new PhoneStateListener() {

		public void onCallStateChanged(int state, String incomingNumber) {
			
			//Log.d(TAG, String.format("onCallStateChanged(state=%d, incomingNumber=%s)", state, incomingNumber));
			switch(state) {
			case TelephonyManager.CALL_STATE_RINGING:
				startRinging(incomingNumber);
				break;

			case TelephonyManager.CALL_STATE_IDLE:
			case TelephonyManager.CALL_STATE_OFFHOOK:
				stopRinging();
				break;
			}
		}
	};
	
	
	protected BroadcastReceiver mReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			
			String action = intent.getAction();
			
			if (action.equals(Intent.ACTION_MEDIA_MOUNTED)){
				
				if (dataCacheWrapper != null)
					dataCacheWrapper = null;
				
				dataCacheWrapper = DataCacheWrapper.resetDataCacheWrapper();
				
			}
		}
	};
	

	public void startRinging(String incoming) {
		// create a notification to help tell user about who is calling
		// first perform lookup against database

		String nice = dataCacheWrapper.revealNumber(incoming);

		if (noteLoop != null)
			stopRinging();
		noteLoop = new NotificationLooper(this, nice);
		noteLoop.start();
	}


	public void stopRinging() {
		if (noteLoop == null)
			return;

		noteLoop.stopLoop();
	}

	//protected static final int NOTIF_ID = 42;
	//protected Notification notif;

	@Override
	public void onCreate() {
		//Log.d(TAG, "creating RevealService");

		// open number database
		//this.numberdb = new NumberDatabase(this);

		this.teleman = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);

        teleman.listen(this.listener, PhoneStateListener.LISTEN_CALL_STATE);
        

        IntentFilter filter =new IntentFilter();
        filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
        filter.addDataScheme("file");
        this.registerReceiver(mReceiver, filter);
        
        Toast.makeText(this, R.string.start_service, Toast.LENGTH_LONG).show();
        
	}

	@Override
	public void onDestroy() {

		// unsubscribe from telephone updates
		this.teleman.listen(listener, PhoneStateListener.LISTEN_NONE);
		this.unregisterReceiver(mReceiver);
	}

	public class RevealBinder extends Binder {
		RevealService getService() {
			return RevealService.this;
		}
	}

	private final IBinder binder = new RevealBinder();

	@Override
	public IBinder onBind(Intent intent) {
		return binder;
	}
}
